
var re = /^-?\d+(\.\d{1,9})?$/;
var oTable1;
var oTable;
$(function(){
	//初始化
	initDataTable();
	initOperTable();
	$('#search').click(function(){
		//添加关联ID校验
		if($('#relateid').val() == ""){
			oTable.ajax.reload();
		}else{
			if(!re.test($('#relateid').val())){
				 layer.alert("请输入正确的关联ID", {
						icon:2,
						closeBtn: 0,
						shift: 3 //动画类型
					});
				 return;
			}
			oTable.ajax.reload();
		}
	});
});
function initDataTable() {
	
	oTable = $('.example').DataTable({
		"dom": '<"top">rt<"bottom"ip><"clear">',
		"ajax": {
			"type": "POST",
			"url": "query.financial",
			"data": function(d) {
				d.relateid = $('#relateid').val(),
				d.clearstatus = $('#clearStatus').val();
			}
		},	
		"processing": true,
        "serverSide": true,
		"ordering": false,
		"aLengthMenu": [5, 10, 20, 40 ],
		"iDisplayLength" : 5,
		"oLanguage": {
			"sUrl": "../static/js/datatable.cn.txt"
		},
		"aoColumns": [
		            {"mDataProp":null},
		            {"mDataProp":"relateid"},
		  			{"mDataProp":"cleardatestr"},
		  			{"mDataProp":"clearstatus"},
		  			{"mDataProp":"nativeprice"},
		  			{"mDataProp":"currentstocks"},
		  			{"mDataProp":"saleprice"},
		  			{"mDataProp":"profit"},
		  			{"mDataProp":"zxprofit"},
		  			{"mDataProp":"nnprofit"},
		  			{"mDataProp":"gysprice"},
		  			{"mDataProp":"loss"},
		  			{"mDataProp":null}
		  		],
		"columnDefs": [{
		  			"render": function (data, type, row) {
		        		  return  row.relateid + '<input type="hidden" id="relateid' + row.relateid + '" name="relateid"  value="'+data+'"/>';
		        	   },
		        	   "targets": 1
		  		},
		  		{
		  			"render": function (data, type, row) {
		  					if(row.cleardatestr == null){
		  						return '        ';
		  					}
		        		  return  data + '<input type="hidden" id="cleanDate' + row.relateid + '" name="relateid"  value="'+data+'"/>';
		        	   },
		        	   "targets": 2
		  		},{
		  			
		  			"render": function (data, type, row) {
		        		   if(row.clearstatus == 1) {
		        			   return '<span>已结算&nbsp;&nbsp;</span>'+'<input type="hidden" id="cleanStatus' + row.relateid + '"value="' +data+ '"/>';
		        		   } else if(row.clearstatus == null){
		        			   return '        ';
		        		   }else{
		        			  return '<span>未结算&nbsp;&nbsp;</span>'+'<input type="hidden" id="cleanStatus' + row.relateid + '"value="' +data+ '"/>';
		        		   }
		        	   },
		        	   "targets": 3
		  		},
		  		{
		  			"render": function (data, type, row) {
		  				if(row.nativeprice == null){
		        			   return '        ';
		        		   }
		        		  return  data + '<input type="hidden" id="nativePrice' + row.relateid + '"value="' +data+ '"/>';
		        	   },
		        	   "targets": 4
		  		},
		  		{
		  			"render": function (data, type, row) {
		  				 if(row.currentstocks == null){
		        			   return '        ';
		        		   }
		        		  return  data + '<input type="hidden" id="currentStocks' + row.relateid + '"value="' +data+ '"/>';
		        	   },
		        	   "targets": 5
		  		},
		  		{
		  			"render": function (data, type, row) {
		  				if(row.saleprice == null){
	  						return '        ';
	  					}
		        		  return  data + '<input type="hidden" id="salePrice' + row.relateid + '"value="' +data+ '"/>';
		        	   },
		        	   "targets": 6
		  		},
		  		{
		  			"render": function (data, type, row) {
		  				if(row.profit == null){
	  						return '        ';
	  					}
		        		  return  data + '<input type="hidden" id="profit' + row.relateid + '"value="' +data+ '"/>';
		        	   },
		        	   "targets": 7
		  		},
		  		{
		  			"render": function (data, type, row) {
		  				if(row.zxprofit == null){
	  						return '        ';
	  					}
		        		  return  data + '<input type="hidden" id="zxProfit' + row.relateid + '"value="' +data+ '"/>';
		        	   },
		        	   "targets": 8
		  		},
		  		{
		  			"render": function (data, type, row) {
		  				if(row.nnprofit == null){
	  						return '        ';
	  					}
		        		  return  data + '<input type="hidden" id="nnProfit' + row.relateid + '"value="' +data+ '"/>';
		        	   },
		        	   "targets": 9
		  		},
		  		{
		  			"render": function (data, type, row) {
		  				if(row.gysprice == null){
	  						return '        ';
	  					}
		        		  return  data + '<input type="hidden" id="gysPrice' + row.relateid + '"value="' +data+ '"/>';
		        	   },
		        	   "targets": 10
		  		},
		  		{
		  				//q
		  				"render": function (data, type, row) {
		  					if(row.loss != null){
		  						return '<input  class="form-control" style="width: 50%;" readonly="readonly" id="loss' + row.relateid + '" name="relateid" type="text" value="'+data+'"/>';
		  					}
		  					return '<input  class="form-control" style="width: 50%;" id="loss' + row.relateid + '" name="relateid" type="text" value=""/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button class="btn btn-success btn-sm"   id = "add_loss'+row.relateid+'" onclick="lossConfirm('+row.relateid+')"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span>确认</button>';
		   				},
		        	   "targets": 11
		  		},{
		  			"render": function (data, type, row) {
		  				if(row.clearstatus == 1){
		  					return '已结算';
		  				}
	  					return '<button class="btn btn-success btn-sm"   id = "confirm'+row.relateid+'" onclick="fconfirm('+row.relateid+')"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span>确认结算</button>';
		  			},
	        	   "targets": 12
		  		}]
		});

	//前台添加序号
	oTable.on('draw.dt',function() {
		oTable.column(0).nodes().each(function(cell, i) {
	        cell.innerHTML = i + 1;
	    });
	}).draw();
	
}

/**确认结算标记*/
var confirmFlag = false;

//var re = /^[0-9]+.?[0-9]*$/;


/**
 * 确认损益
 * @param id
 */
function lossConfirm(id){
	//获取损益价格
	var loss = $("#loss"+id).val();
	//alert(loss +"          "+isNaN(loss));
	if(!re.test(loss)){
		 layer.alert("请输入正确的损益值", {
				icon:2,
				closeBtn: 0,
				shift: 3 //动画类型
			});
		 return;
	}
	
	layer.confirm("请核对损益，确定后不得修改！",{
		icon: 3,
		btn:['确定','我再想想']//按钮
		},function(){
			
			
			//设置输入框只读
			$("#loss"+id).attr("readonly","readonly");
			
			$.ajax({
        		cache: false,
                type: "POST",
                url: "update.loss",
                data: {"relateid":id,
       			        "loss":loss,
       			        },
                async: false,
                dataType:"json",
                error: function(request) {
                    layer.alert("失败", {
							icon:2,
							closeBtn: 0,
							shift: 3 //动画类型
						});
                },
                success: function(data) {
                	confirmFlag = true;
                	layer.alert(data.msg, {
							icon:data.status,
							closeBtn: 0,
							shift: 3 //动画类型
						});
                	 oTable.ajax.reload();
                	 oTable1.ajax.reload();
                }
            })  
		});
}





/**
 * 确认结算
 */
function fconfirm(id){
//	var loss = $("#loss"+id).val();
//	if(loss == "" || !re.test(loss)){
//		 layer.alert("请确认损益是否正确", {
//				icon:2,
//				closeBtn: 0,
//				shift: 3 //动画类型
//			});
//		return;
//	}
	
	 var str = '{'+'"relateid":'+$("#relateid"+id).val()+','+'"nativePrice":'+$("#nativePrice"+id).val() +','
	   +'"currentStocks":'+$("#currentStocks"+id).val()+','+'"salePrice":'+$("#salePrice"+id).val()+','+'"profit":'+$("#profit"+id).val()+','
	   +'"zxProfit":'+$("#zxProfit"+id).val()+','+'"nnProfit":'+$("#nnProfit"+id).val()+','
	   +'"gysPrice":'+$("#gysPrice"+id).val()+'}';
	
	layer.confirm("请核对信息，确认结算后不得修改！",{
		icon: 3,
		btn:['确定','我再想想']//按钮
		},function(){
				 $.ajax({
					 cache:false,
					 type:"POST",
					 url:"update.cleanStatus",
					 data:{"info":str},
					 async:false,
					 error: function(request) {
		                    layer.alert("失败", {
									icon:2,
									closeBtn: 0,
									shift: 3 //动画类型
								});
		                },
		              success: function(data) {
		                	layer.alert(data.msg, {
									icon:data.status,
									closeBtn: 0,
									shift: 3 //动画类型
								});
		                	 oTable.ajax.reload();
		                	 oTable1.ajax.reload();
		              }
				 });}
		);
}






function exportExcel(){
	//通过获取到的 relateid 和 cleanStatus 从数据库中查找
	
	//添加关联ID校验
	if($('#relateid').val() == ""){
		window.open("export.excel?relateid="+$('#relateid').val()+"&cleanStatus="+$('#clearStatus').val());
	}else{
		if(!re.test($('#relateid').val())){
			 layer.alert("请输入正确的关联ID", {
					icon:2,
					closeBtn: 0,
					shift: 3 //动画类型
				});
			 return;
		}
		window.open("export.excel?relateid="+$('#relateid').val()+"&cleanStatus="+$('#clearStatus').val());
	}

}


function initOperTable() {	
	oTable1 =$('.example1').DataTable({
		"dom": '<"top">rt<"bottom"ip><"clear">',
		"ajax": {
			"type": "POST",
			"url": "query.operation",
			"data": function(d){
	}
		},	
		"processing": true,
        "serverSide": true,
		"ordering": false,
		"aLengthMenu": [5, 10, 20, 40 ],
		"iDisplayLength" : 10,
		"oLanguage": {
			"sUrl": "../static/js/datatable.cn.txt"
		},
		"aoColumns": [
		            {"mDataProp":"relateid"},
		  			{"mDataProp":"operation"},
		  			{"mDataProp":"dateoperation"},
		  			{"mDataProp":"operator"},
		  		],"columnDefs": [{
		  			"searchable": false,
		            "orderable": false,
		            "targets": 0
		  		}]
		});
}

